﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace FrameworkXNA
{
    class Object2D
    {
        public String name;
        public SpriteEffects spriteEffects;
        public Texture2D texture;
        public float x, y, width, height;
        public float rotation, alpha, scaleX, scaleY;
        public Color color;
        public bool inverted, visible;
        public Rectangle sourceRectangle, collisionBounds;
        public Vector2 originVector;

        public Object2D(Texture2D texture)
        {
            this.texture = texture;
            spriteEffects = SpriteEffects.None;
            x = y = 0;
            scaleX = scaleY = 1;
            rotation = 0;
            alpha = 1;
            color = Color.White;
            inverted = false;
            visible = true;

            width = texture.Width;
            height = texture.Height;

            this.sourceRectangle = new Rectangle(0, 0, texture.Width, texture.Height);

            originVector = new Vector2(sourceRectangle.Width / 2, sourceRectangle.Height / 2);
        }

        public void setHeight(float height)
        {
            this.height = height;
            scaleY = height / sourceRectangle.Height;
        }

        public void setWidth(float width)
        {
            this.width = width;
            scaleX = width / sourceRectangle.Width;
        }

        public void ChangeTexture(Texture2D texture)
        {
            this.texture = texture;
            sourceRectangle = new Rectangle(0, 0, texture.Width, texture.Height);
        }

        public virtual void CalcularRetangulo()
        {
            collisionBounds = new Rectangle(
                (int)(this.x - (sourceRectangle.Width / 2) * scaleX),
                (int)(this.y - (sourceRectangle.Height / 2) * scaleY),
                (int)(sourceRectangle.Width * scaleX),
                (int)(sourceRectangle.Height * scaleY)
                );
        }

        public Boolean hitTestObject(Object2D objeto)
        {
            this.CalcularRetangulo();
            objeto.CalcularRetangulo();

            return this.collisionBounds.Intersects(objeto.collisionBounds);

        }

        public virtual Boolean Clicked()
        {
            CalcularRetangulo();

            if (this.collisionBounds.Contains(Controller.mousePoint()) && Controller.MouseLeftPressed())
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        public virtual void Update()
        {

        }

        public virtual void Draw(SpriteBatch spritebatch)
        {
            if (visible)
            {
                spritebatch.Draw
                (
                    texture,
                    new Vector2(x, y),
                    sourceRectangle,
                    Color.Multiply(color, alpha),
                    MathHelper.ToRadians(rotation),
                    originVector,
                    new Vector2(scaleX, scaleY),
                    (inverted ? SpriteEffects.FlipHorizontally : SpriteEffects.None),
                    0
                );
            }
        }
    }
}
